<template>
  <div class="login-setting">
    <el-form
      ref="userForm"
      :model="userInfo"
      :rules="rules"
      label-width="120px"
      style="margin-left: 120px; margin-top: 30px"
    >
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="userInfo.username" style="width:300px" />
      </el-form-item>

      <el-form-item label="密码:" prop="password">
        <el-input v-model="userInfo.password" type="password" style="width:300px" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="updateInfo">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { saveUserInfoAPI } from '@/api/user'
export default {
  name: 'LoginSetting',
  data() {
    return {
      // 用户基本信息
      userInfo: {
        username: '',
        password: ''
      },
      // 表单校验规则
      rules: {
        username: [{ required: true, message: '用户名不能为空', triggler: 'blur' }],
        password: [{ required: true, message: '密码不能为空', triggler: 'blur' }]
      },
      id: ''
    }
  },
  computed: {
    ...mapGetters({ 'userinfoo': 'userInfo' })
  },
  created() {
    this.userInfo = this.userinfoo
  },
  methods: {
    // 点击更新按钮,触发事件
    updateInfo(res) {
      // 进行兜底验证
      this.$refs.userForm.validate(async bool => {
        if (!bool) return
        const res = await saveUserInfoAPI(this.userInfo.id, this.userInfo)
        this.$store.commit('user/getUserInfo', res.data)
      })
    }
  }}
</script>

<style lang="scss" scoped></style>
